<?php
require_once('init.php');
$type_list = array("list","add");

$type = trim($_GET['type']);
if (!in_array($type, $type_list)) $type = "list";

$cate_mng = new MerchantCate();

$html = <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>商品分类</title>
<link rel="stylesheet" href="../common/style.css" type="text/css" />
</head>
<body>
EOT;

$cate_mng = new MerchantCate();
$cate_list = $cate_mng->getAll();
$cate_options = $cate_mng->getOptions();

switch ($type)
{
	case 'list':
		$html .= '<div><a href="../">首页</a>&nbsp;<a href="?type=add">添加分类</a></div>';

		$page = $_GET['page'] ? (int)$_GET['page'] : 0;
		$size = $_GET['size'] ? (int)$_GET['size'] : 20;
		$list = $cate_mng->getList($page, $size);
		if ($list) {
			$html .= '<table><thead><th>分类ID</th><th>分类名称</th><th>上级分类</th></thead>';
			foreach ($list as $row) {
				$html .= "<tr><td>".$row['id']."</td><td>".$row['name']."</td><td>".$cate_list[$row['pid']]['name']."</td></tr>";
			}
			$html .= "</table>";
		} else {
			$html .= '<br/>没有商品分类信息！';
		}
		break;
	case 'add':
		$html .= '<div><a href="../">首页</a>&nbsp;<a href="?type=list">分类列表</a></div>';

		if ($_POST['Cate'])
		{
			$post = $_POST['Cate'];
			$insert_data = array(
				'pid'=>(int)$post['pid'],
				'name'=>addslashes($post['name']),
				'modtime'=>date('Y-m-d H:i:s')
			);
			$ok = $cate_mng->insert($insert_data);
			header('Location:?type=list');
		}

		$html .= '<form action="" method="post"><table>';
		$html .= '<tr><td>上级分类：</td><td><select name="Cate[pid]"><option>请选择</option>';
		$html .= $cate_options;
		$html .= '</select></td>';
		$html .= '<tr><td>分类名称：</td><td><input type="text" name="Cate[name]" /></td>';
		$html .= '<tr><td colspan=2><input type="submit" value="提交" /></td>';
		$html .= '</table></form>';
		break;
}

$html .= <<<EOT
</body>
</html>
EOT;

echo $html;
?>
